package org.frvi.goldspring.business.dao;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by IntelliJ IDEA.
 * User: Frank
 * Date: Oct 13, 2009
 * Time: 9:21:05 PM
 */
@Transactional
public abstract class AbstractHibernateDaoImpl<T extends DataObject> implements Dao<T> {

    SessionFactory sessionFactory;

    public T save(T object) {
        getSessionFactory().getCurrentSession().persist(object);
        return object;
    }

    @SuppressWarnings("unchecked")
    public T load(Long id) {
        return (T) getSessionFactory().getCurrentSession().load(getDomainObject(), id);

    }

    @SuppressWarnings("unchecked")
    public T load(Class type, Long id) {
        return (T) getSessionFactory().getCurrentSession().load(type, id);
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Required @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    abstract protected Class getDomainObject();

}
